//
//  YRJAllHotCityViewController.m
//  YanRenjie
//
//  Created by dllo on 15/10/28.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJAllHotCityViewController.h"
#import "YRJTripHeader.h"


@interface YRJAllHotCityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *allHotCityArr;
@property(nonatomic, retain)MBProgressHUD *hud;
@end

NSInteger allHotCityPageIndex = 1;
@implementation YRJAllHotCityViewController
- (void)dealloc {
    [_string release];
    [_lat release];
    [_lon release];
    [_tableView release];
    [_hud release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"******%@", self.string);
    self.title = @"全部热门城市";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(backHomeAction:)];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = (120.0 / 667) * HEIGHT;
    // tableView  注册
    [self.tableView registerClass:[YRJAllHotCityTableViewCell class] forCellReuseIdentifier:@"allHotCityReuse"];
#warning 添加上拉加载, 下拉刷新功能
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullDownToRefresh)];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        allHotCityPageIndex++;
        [self pullUpToLoading];
    }];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"Loading...";
    [self createData];
}


- (void)createData {
    NSString *tempString = @"http://open.qyer.com/place/city/get_city_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.883474&lon=121.544454&page=";
    NSString *strURL = [NSString stringWithFormat:@"%@%ld%@%@", tempString, allHotCityPageIndex, @"&countryid=", self.string];
    NSLog(@"%@", strURL);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.allHotCityArr = [YRJAllHotCityModel baseModelByArr:dic[@"data"]];
        [self.tableView reloadData];
        self.hud.hidden = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


#pragma mark - 上拉加载, 下拉刷新
- (void)pullUpToLoading {
    NSString *tempString = @"http://open.qyer.com/place/city/get_city_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867739013144832&track_app_version=6.8.1&track_app_channel=hiapk&track_device_info=bbk77_cu_ics2&track_os=Android4.0.4&app_installtime=1445390305001&lat=38.883474&lon=121.544454&page=";
    NSString *strURL = [NSString stringWithFormat:@"%@%ld%@%@", tempString, allHotCityPageIndex, @"&countryid=", self.string];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *tempArray = [NSArray array];
        tempArray = [YRJAllHotCityModel baseModelByArr:dic[@"data"]];
        for (YRJAllHotCityModel *tempModel in tempArray) {
            [self.allHotCityArr addObject:tempModel];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        self.hud.hidden = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"wo zou");
    }];
}


- (void)pullDownToRefresh {
    [self.allHotCityArr removeAllObjects];
    [self pullUpToLoading];
}



#pragma mark - tableView 两个必须实现的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allHotCityArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YRJAllHotCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allHotCityReuse" forIndexPath:indexPath];
    // 刷新的时候判断数组是否为空, 因为在刷新前已经清空了数组, 不判断就会crash
    if (self.allHotCityArr.count != 0) {
        [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.allHotCityArr[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
        cell.representativeLabel.text = [self.allHotCityArr[indexPath.row] representative];
        cell.beenstrLabel.text = [self.allHotCityArr[indexPath.row] beenstr];
        cell.catenameLabel.text = [self.allHotCityArr[indexPath.row] catename];
        cell.catename_enLabel.text = [self.allHotCityArr[indexPath.row] catename_en];
        
    }
    return cell;
}


#pragma mark - tableView cell 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YRJCityDetailViewController *cityDetailVC = [[YRJCityDetailViewController alloc] init];
    cityDetailVC.string = (NSString *)[self.allHotCityArr[indexPath.row] id];
    cityDetailVC.cityName = [self.allHotCityArr[indexPath.row] catename];
    cityDetailVC.cityModel = self.allHotCityArr[indexPath.row];
    [self.navigationController pushViewController:cityDetailVC animated:YES];
    [cityDetailVC release];
}


#pragma mark - 返回首页操作
- (void)backHomeAction:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
